//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    page_head: [
      { img: '/pages/icon/1_07.png', text: '待付款', num: 0 },
      { img: '/pages/icon/1_09.png', text: '待成团', num: 0 },
      { img: '/pages/icon/1_11.png', text: '待发货', num: 0 },
      { img: '/pages/icon/1_13.png', text: '待收货', num: 0 },
      { img: '/pages/icon/1_15.png', text: '待评价', num: 0 },
    ],
    localData: {
      icon: [
        "/pages/icon/1_99.png",
        "/pages/icon/diejiajing.png",
        "/pages/icon/diejiajing.png",
        "/pages/icon/1_99.png",
        "/pages/icon/sale.png",
        "/pages/icon/1_30.png",
        "/pages/icon/1_32.png",
        "/pages/icon/1_34.png",
        "/pages/icon/1_36.png",
        "/pages/icon/1_38.png",
        "/pages/icon/diejiajing.png",
      ],
      title: ["我的骆驼卡", "现金红包", "叠加金", "立减金", "优惠劵", "我的收藏", "足迹", "我的拼团", "退款售后", "兑换记录", "小游戏", "意见反馈",]
    },
    load_status: true,
  },
  onLoad: function () {
    var that = this;

    that.setData({ load_status: false, })


    app.app_load(load_fn);

    function load_fn() {
      wx.showLoading({
        title: '加载中...',
      });

      console.log(wx.getStorageSync('token_key'));


      wx.request({
        url: app.url + 'api_user/get_order_state_count',
        data: {},
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (json) {
          wx.hideLoading();
          console.log(json)
          var i = 0;
          for (var x in json.data.result) {
            that.data.page_head[i++].num = json.data.result[x];
          }
          that.setData({
            page_head: that.data.page_head,
          })

        }
      })
      // 调用应用实例的方法获取全局数据
      wx.getStorage({
        key: 'userInfo',
        success: function (res) {
          that.setData({
            avatarUrl: res.data.avatarUrl,
            nickName: res.data.nickName,
          })
        }
      })
    }
  },
  // 地址按钮
  site_btn: function () {

    // wx.chooseAddress({
    //   success: function (res) {
    //     console.log(res);
    //     // 添加至后台
    //     wx.request({
    //       url: app.url + 'api_user/chooseWeChatAddress',
    //       data: {
    //         accept_name: res.userName,
    //         province: res.provinceName,
    //         city: res.cityName,
    //         area: res.countyName,
    //         address: res.detailInfo,
    //         zip: res.postalCode,
    //         mobile: res.telNumber,
    //       },
    //       header: {
    //         'content-type': 'application/json',
    //         'token': wx.getStorageSync('token_key')

    //       }, success: function (json) {
    //         if (json.data.done == true) {

    //         } else {
    //           wx.showModal({
    //             title: '提示',
    //             content: json.data.message,
    //           })
    //         }
    //       }
    //     })
    //     console.log(res);
    //   }, fail: function (res) {
    //     console.log(res)
    //     wx.showModal({
    //       title: '温馨提醒',
    //       content: '需要获取您的地理位置才能使用小程序',
    //       cancelText: '取消',
    //       confirmText: '获取位置',
    //       success: function (res) {
    //         if (res.confirm) {
    //           getAuthor(that);
    //         } else if (res.cancel) {
    //           // getAuthor(that);
    //         }
    //       }
    //     })
    //   }

    // })


    wx.navigateTo({
      url: '/pages/address/index/index',
    })
  },
  // 订单按钮
  orderTAP: function (e) {
    var fid = e.currentTarget.id;
    console.log(fid);
    var url = '';
    if (fid == 2) {
      url = '/pages/mygroup/mygroup?fid=' + fid;
    } else if (fid > 2) {
      url = '/pages/account/order/index?fid=' + (++fid);
    } else {
      url = '/pages/account/order/index?fid=' + fid;
    }
    wx.navigateTo({
      url: url,
    })
  },

  //列表按钮
  lists_btn: function (e) {
    var fid = e.currentTarget.id;
    console.log(fid);
    var url = '';
    if (fid == 0) {
      console.log("我的骆驼卡")
      url = '/pages/camel_card/my_camelCard/index';
    } else if (fid == 1) {
      console.log("现金红包")
      url = '/pages/voice_box/mention/index?index=0';
    } else if (fid == 2) {
      console.log("叠加金")
      url = '/pages/account/discount/index?id=1';
    } else if (fid == 3) {
      console.log("立减金")
      // url = '/pages/account/reducefund/myreducefund/index';
      url = '/pages/account/discount/index?id=2';
    } else if (fid == 4) {
      console.log("优惠劵")
      url = '/pages/account/discount/index?id=3';
    } else if (fid == 5) {
      console.log("我的收藏")
      url = '/pages/account/keep/index';
    } else if (fid == 6) {
      console.log("足迹")
      url = '/pages/account/footmark/index';
    } else if (fid == 7) {
      console.log("我的拼团")
      url = '/pages/mygroup/mygroup';
    } else if (fid == 8) {
      console.log("退款售后")
      url = '/pages/account/reject/index';
    } else if (fid == 9) {
      console.log("兑换记录")
      url = '/pages/blessing/my_blessing/index';
    } else if (fid == 10) {
      console.log("小游戏")
      url = '/pages/voice_box/index/index';
    } else if (fid == 11) {
      console.log("意见反馈")
      url = '/pages/account/feedback/index';
    }
    wx.navigateTo({
      url: url,
    })
  },
  // 底部分栏
  l_home: function (e) {
    wx.reLaunch({
      url: '/pages/tabBar/index/index',
    })
  },
  // 
  l_cate: function (e) {
    wx.reLaunch({
      url: '/pages/category/index'
    })
  },

  l_cart: function (e) {
    wx.reLaunch({
      url: '/pages/shop/index'
    })
  },
  l_account: function (e) {
    wx.reLaunch({
      url: '/pages/tabBar/account/index'
    })
  },
  kfbtn: function () {
    app.wxconsole(55)
  },

  autoFormId: function (e) {
    wx.request({
      url: app.url + "api_team/collectFormId",
      data: {
        formId: e.detail.formId
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (result) { }
    });
  },
  onShow: function () {
    var that = this;
    if (that.data.load_status) {
      that.onLoad();
    }
  },
})

function getAuthor(that) {
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.userLocation']) {
        wx.openSetting({
          success: (res) => {

          }
        })
      }
    }
  })
}
